package com.ggs.starbug.config.rabbitmq;

import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

import static com.ggs.starbug.config.rabbitmq.RabbitMQConstants.*;

/**
 * @Author lianghaohui
 * @Date 2021/12/29 16:20
 * @Description 延迟交换机、队列配置
 */
@Slf4j
@Configuration
public class DelayedExchangeQueueConfig {


    @Bean
    public CustomExchange delayedExchange() {
        Map<String, Object> arguments = Maps.newHashMap();
        arguments.put("x-delayed-type", "direct");  //指定延迟交换机的类型为direct
        /**
         * 1.交换机名称
         * 2.交换机类型
         * 3.是否需要持久化
         * 4.是否需要自动删除
         * 5.其他参数
         **/
        return new CustomExchange(DELAYED_EXCHANGE, "x-delayed-message", true, false, arguments);
    }

    @Bean
    public Queue delayedQueue() {
        return QueueBuilder.durable(DELAYED_QUEUE).build();
    }

    @Bean
    public Binding delayedQueueExchangeBinding(Queue delayedQueue, CustomExchange delayedExchange) {
        return BindingBuilder.bind(delayedQueue).to(delayedExchange).with(DELAYED_ROUTING_KEY).noargs();
    }

}
